//
//  SupplementaryView.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/6/25.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "SupplementaryView.h"
#import "Media.h"



@interface SupplementaryView ()

@property (nonatomic, strong) UILabel *label;

@end





@implementation SupplementaryView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _label = [self supplementaryLabel];
        [self addSubview:_label];
        
        self.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_label
                                                         attribute:NSLayoutAttributeCenterX
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterX
                                                        multiplier:1.0f
                                                          constant:0.0f]];
        
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_label
                                                         attribute:NSLayoutAttributeCenterY
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterY
                                                        multiplier:1.0f
                                                          constant:0.0f]];
    }
    
    return self;
}

- (UILabel *)supplementaryLabel
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectInset(self.bounds, 8.0, 8.0)];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:18.0];
    label.textAlignment = NSTextAlignmentCenter;
    label.translatesAutoresizingMaskIntoConstraints = NO;
    
    return label;
}

- (void)bind:(NSArray *)medias
{
    NSInteger numberOfVideos = 0;
    NSInteger numberOfPhotos = 0;
    for (Media *media in medias) {
        if (media.isVideo.boolValue) {
            numberOfVideos++;
        } else {
            numberOfPhotos++;
        }
    }

    if (numberOfVideos == 0)
        self.label.text = [NSString stringWithFormat:NSLocalizedString(@"%ld Photos", nil), (long)numberOfPhotos];
    else if (numberOfPhotos == 0)
        self.label.text = [NSString stringWithFormat:NSLocalizedString(@"%ld Videos", nil), (long)numberOfVideos];
    else
        self.label.text = [NSString stringWithFormat:NSLocalizedString(@"%ld Photos, %ld Videos", nil), (long)numberOfPhotos, (long)numberOfVideos];
}

@end
